package com.ants.modules.extension.controller;

import com.ants.common.annotation.AutoLog;
import com.ants.common.annotation.RateLimiter;
import com.ants.common.system.query.QueryGenerator;
import com.ants.common.system.result.Result;
import com.ants.modules.extension.entity.Extension;
import com.ants.modules.extension.service.ExtensionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * TODO     推广
 * Author Chen
 * Date   2021/9/7 18:44
 */

@Slf4j
@Api(tags = "推广")
@RestController
@RequestMapping("/extension")
public class ExtensionController {

    @Autowired
    ExtensionService extensionService;


    @RateLimiter
    @AutoLog(value = "推广-列表")
    @ApiOperation(value = "推广-列表", notes = "推广-列表")
    @GetMapping("/list")
    public Result<?> queryPageList(Extension extension,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<Extension> queryWrapper = QueryGenerator.initQueryWrapper(extension, req.getParameterMap());
        Page<Extension> page = new Page<Extension>(pageNo, pageSize);
        IPage<Extension> pageList = extensionService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    @RateLimiter
    @AutoLog(value = "推广-列表")
    @ApiOperation(value = "推广-列表", notes = "推广-列表")
    @GetMapping("/homeAdList")
    public Result<?> homeAdList(@RequestParam Integer randNum) {
        List<Extension> list = extensionService.getHomeAdList(randNum);
        return Result.ok(list);
    }
    @RateLimiter
    @AutoLog(value = "推广-列表")
    @ApiOperation(value = "推广-列表", notes = "推广-列表")
    @GetMapping("/asideAd")
    public Result<?> asideAd() {
        Extension result = extensionService.getAsideAdList();
        return Result.ok(result);
    }

    @AutoLog(value = "推广-列表")
    @ApiOperation(value = "推广-列表", notes = "推广-列表")
    @GetMapping("/initFriendLinks")
    public Result<?> initFriendLinks(Extension extension,
                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                     HttpServletRequest req) {
        QueryWrapper<Extension> queryWrapper = QueryGenerator.initQueryWrapper(extension, req.getParameterMap());
        queryWrapper.select(Extension.class, info -> !info.getColumn().equals("email"));
        List<Extension> list = extensionService.list(queryWrapper);
        return Result.ok(list);
    }

    /**
     * 添加
     *
     * @param extension
     * @return
     */
    @AutoLog(value = "推广-添加")
    @ApiOperation(value = "推广-添加", notes = "推广-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody Extension extension) {
        boolean save = extensionService.save(extension);
        if (save) {
            return Result.ok("操作成功！");
        }
        return Result.error("操作失败！");
    }


    /**
     * 编辑
     *
     * @param extension
     * @return
     */
    @AutoLog(value = "推广-编辑")
    @ApiOperation(value = "推广-编辑", notes = "推广-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody Extension extension) {
        boolean b = extensionService.updateById(extension);
        if (b) {
            return Result.ok("操作成功!");
        }
        return Result.error("操作失败!");
    }

    /**
     * @param id
     * @return
     * @功能：删除
     */
    @DeleteMapping("/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        boolean ok = extensionService.removeById(id);
        if (ok) {
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }

    /**
     * @param ids
     * @return
     * @功能：批量删除
     */
    @DeleteMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        boolean ok = extensionService.removeByIds(Arrays.asList(ids.split(",")));
        if (ok) {
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }


}
